package com.pdg.mcplugin.payd2mine;

import java.util.List;

import org.bukkit.Material;
import org.bukkit.command.CommandSender;

import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.baseclasses.CommandProcessorBase;

public class CommandProcessor extends CommandProcessorBase<Payd2Mine> {

	private static final String KEY_RELOAD = "reload";
	private static final String KEY_CACHE_SIZE = "cache-size";
	private static final String KEY_PURGE_CACHE = "purge-cache";
	private static final String KEY_PAYOUT = "payout";
	private static final String KEY_VERSION = "version";
	private static final String KEY_LIST = "list";
	private static final String MESSAGE_RELOAD = "Reloading";
	private static final String FORMAT_CACHE_SIZE = "The size of the block cache is %d records.";
	private static final String MESSAGE_PURGE_CACHE = "The block cache has been purged.";
	private static final String MESSAGE_PAYOUT = "Paid out pending payments.";
	private static final String FORMAT_VERSION = "Version installed: %s";
	private static final String FORMAT_LIST_LINE = "%s: %s";

	public CommandProcessor(Payd2Mine thePlugin) {
		super(thePlugin, new String[]{
				"/p2m - help for p2m(you are here)",
				"/p2m reload - reloads everything",
				"/p2m cache-size - reports the block cache size",
				"/p2m purge-cache - clears the block cache",
				"/p2m payout - pays pending payments",
				"/p2m version - shows the version",
				"/p2m list - lists materials and associated values"
		});
	}

	@Override
	protected boolean specialProcessCommand(CommandSender theCommandSender,
			String theSubCommand, ArgumentList theArgumentList) {
		if(theSubCommand.equalsIgnoreCase(KEY_RELOAD)){
			return handleReloadSubcommand(theCommandSender);
		}else if(theSubCommand.equalsIgnoreCase(KEY_CACHE_SIZE)){
			return handleCacheSizeSubcommand(theCommandSender);
		}else if(theSubCommand.equalsIgnoreCase(KEY_PURGE_CACHE)){
			return handlePurgeCacheSubcommand(theCommandSender);
		}else if(theSubCommand.equalsIgnoreCase(KEY_PAYOUT)){
			return handlePayoutSubcommand(theCommandSender);
		}else if(theSubCommand.equalsIgnoreCase(KEY_VERSION)){
			return handleVersionSubcommand(theCommandSender);
		}else if(theSubCommand.equalsIgnoreCase(KEY_LIST)){
			return handleListSubcommand(theCommandSender);
		}else{
			return false;
			
		}
	}

	private boolean handleListSubcommand(CommandSender theCommandSender) {
		if(!getPlugin().getPermissionChecker().check(theCommandSender, P2MPermissions.LIST)) return true;
		List<Material> theKeys = getPlugin().getMaterialTable().getKeys();
		for(Material theKey : theKeys){
			double theValue = getPlugin().getMaterialTable().getAmount(theKey);
			getPlugin().sendInformationalMessage(theCommandSender, String.format(FORMAT_LIST_LINE,theKey.toString(),getPlugin().getEconomy().format(theValue)));
		}
		return true;
	}

	private boolean handleVersionSubcommand(CommandSender theCommandSender) {
		if(!getPlugin().getPermissionChecker().check(theCommandSender, P2MPermissions.VERSION)) return true;
		String theVersion = getPlugin().getDescription().getVersion();
		getPlugin().sendInformationalMessage(theCommandSender, String.format(FORMAT_VERSION, theVersion));
		return true;
	}

	private boolean handlePayoutSubcommand(CommandSender theCommandSender) {
		if(!getPlugin().getPermissionChecker().check(theCommandSender, P2MPermissions.PAYOUT)) return true;
		getPlugin().getDataCache().payPlayers();
		getPlugin().sendInformationalMessage(theCommandSender, MESSAGE_PAYOUT);
		return true;
	}

	private boolean handlePurgeCacheSubcommand(CommandSender theCommandSender) {
		if(!getPlugin().getPermissionChecker().check(theCommandSender, P2MPermissions.PURGE_CACHE)) return true;
		getPlugin().getDataCache().clearCachedBlocks();
		getPlugin().sendInformationalMessage(theCommandSender, MESSAGE_PURGE_CACHE);
		return true;
	}

	private boolean handleCacheSizeSubcommand(CommandSender theCommandSender) {
		if(!getPlugin().getPermissionChecker().check(theCommandSender, P2MPermissions.CACHE_SIZE)) return true;
		int theSize = getPlugin().getDataCache().getBlockCacheSize();
		getPlugin().sendInformationalMessage(theCommandSender, String.format(FORMAT_CACHE_SIZE, theSize));
		return true;
	}

	private boolean handleReloadSubcommand(CommandSender theCommandSender) {
		if(!getPlugin().getPermissionChecker().check(theCommandSender, P2MPermissions.RELOAD)) return true;
		getPlugin().sendInformationalMessage(theCommandSender, MESSAGE_RELOAD);
		getPlugin().reinitialize();
		return true;
	}

}
